package com.mine.frame.mylandemo.ui.fgm

import android.os.Bundle
import com.blankj.utilcode.util.SnackbarUtils
import com.mine.frame.common.base.BaseFragment
import com.mine.frame.mylandemo.R
import com.mine.frame.mylandemo.adapter.forick.Panel3In1Adapter
import com.mine.frame.mylandemo.entity.forick.ControlBean
import com.mine.frame.mylandemo.mvp.iview.ITwo
import com.mine.frame.mylandemo.mvp.presenter.TwoPresenter
import com.third.device.mg.serialport.service.MGSocketService
import kotlinx.android.synthetic.main.fgm_recycler.*

/**
 * @Date 2022/4/12
 * Description:
 */
class CommandFgm :BaseFragment<ITwo ,TwoPresenter>() ,ITwo {


    private  var ipStr:String = ""
    private  var port:Int = 0

    private val mAdapter by lazy { Panel3In1Adapter() }


    override fun initLayoutId(): Int = R.layout.fgm_recycler

    override fun initPresenter(): TwoPresenter = TwoPresenter(this)

    override fun initLayoutAfter(savedInstanceState: Bundle?) {


    }

    /**
     * 0是升降吊柜
     * 1是平移导台
     */
    fun onInitData(ip:String, port:Int, type : Int){
        this.ipStr = ip
        this.port = port
        if (type == 0){
            initRecycler()
            mPresenter.createControlDataLifting()
        }else{
            initRecycler()
            mPresenter.createControlData()
        }
    }


    private fun initRecycler() {
        mRecycler.adapter = mAdapter
        mAdapter.setOnItemClickListener { _, _, position ->
            val controlBean = mAdapter.data[position]
            if (!controlBean.isHeader) {
                controlBean.command?.let {
                    MGSocketService.get().write(ipStr, port, it)
                } ?: let {
                    SnackbarUtils.with(mRecycler).setMessage("指令未空").showWarning(true)
                }
            }
        }
    }


    override fun initControlListTranslationDesk(list: MutableList<ControlBean>) {
        mAdapter.setList(list)
    }

    override fun initControlListLifting(list: MutableList<ControlBean>) {
        mAdapter.setList(list)
    }
}